package com.jp.knowledge.refresh;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefresh;
import com.jp.knowledge.R;
import com.jp.knowledge.util.TimeUtil;

/**
 * Created by jiayen on 16/11/24.
 *
 * 自定义刷新头部
 */
public class JpRefreshView extends FrameLayout implements CanRefresh {
    private View mView;
    private Context mContext;


    private final int ROTATION_ANIMATION_DURATION = 1200;

    private  Animation mRotateAnimation;


    private ImageView ivRotate;

    private  int rotateHight ;

    private TextView  refreshTime;

    private TextView refreshTemp;

    private long lastTime;//记录上一次刷新时间

    private boolean isHeader;
    private boolean isLoadComplete;

    public boolean isLoadComplete() {
        return isLoadComplete;
    }

    public void setLoadComplete(boolean loadComplete) {
        isLoadComplete = loadComplete;
    }

    public JpRefreshView(Context context) {
        this(context,null);
    }

    public JpRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public JpRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        initView();
    }

    private void initView() {


        mRotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rotateHight=(int) getResources().getDimension(com.canyinghao.canrefresh.R.dimen.refresh_height_rotate);

        mView = LayoutInflater.from(mContext).inflate(R.layout.jp_refresh_layout,null);
        addView(mView);

        FrameLayout.LayoutParams params = (LayoutParams) mView.getLayoutParams();
        params.height = rotateHight;
        mView.setLayoutParams(params);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        refreshTime = (TextView) findViewById(R.id.refresh_time);
        ivRotate = (ImageView) findViewById(R.id.refresh_loading);
        refreshTemp = (TextView) findViewById(R.id.refresh_temp);
        ivRotate.setScaleType(ImageView.ScaleType.MATRIX);
        mRotateAnimation.setInterpolator(new LinearInterpolator());
        mRotateAnimation.setDuration(ROTATION_ANIMATION_DURATION);
        mRotateAnimation.setRepeatCount(Animation.INFINITE);
        mRotateAnimation.setRepeatMode(Animation.RESTART);

        refreshTime.setText("最后更新:"+ TimeUtil.getRefreshTime(lastTime==0?System.currentTimeMillis():lastTime));
    }

    /**
     * 重置
     */
    @Override
    public void onReset() {
        ivRotate.clearAnimation();
        onPositionChange(0);
    }

    /**
     * 放手后
     */
    @Override
    public void onRelease() {
        if(isHeader || !isLoadComplete) {
            ivRotate.startAnimation(mRotateAnimation);
        }
    }

    /**
     * 下拉高度大于头部高度
     */
    @Override
    public void onPrepare() {

    }

    /**
     * 下拉高度与头部高度比例
     *
     * @param currentPercent
     */
    @Override
    public void onPositionChange(float currentPercent) {
        ViewCompat.setRotation(ivRotate,currentPercent*rotateHight);
        if(isHeader) {
            refreshTemp.setText(currentPercent < 1 ? "下拉可以刷新" : "松开立即刷新");
        } else {
            ivRotate.setVisibility(isLoadComplete ? View.GONE : View.VISIBLE);
            refreshTemp.setText(isLoadComplete ? "已经全部加载完毕" : currentPercent < 1 ? "上拉可以加载更多" : "松开立即加载更多");
        }
    }

    /**
     * 刷新完成
     */
    @Override
    public void onComplete() {
        mRotateAnimation.cancel();
        long curTime = System.currentTimeMillis();//记录当前时间
        if(lastTime==0){
            lastTime=curTime;
        }
        refreshTime.setText("最后更新:"+ TimeUtil.getRefreshTime(lastTime));
        lastTime = curTime;
    }

    /**
     * 是下拉还是上拉
     *
     * @param isHeader
     */
    @Override
    public void setIsHeaderOrFooter(boolean isHeader) {
        this.isHeader = isHeader;
        refreshTime.setVisibility(isHeader ? View.VISIBLE : View.GONE);
    }
}
